<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
</head>

<body>
  <script>
    // 柯里化思想
    function add() {

      // 接收初始化参数
      let args = [...arguments]

      let inner = () => {
        args.push(...arguments)//args最终就是接收了所有参数的一个数组
        return inner
      }

      inner.toString = () => {
        // 用args累加和
        return args.reduce((prev, item) => prev + item)
      }
      return inner
    }
    console.log(+add(1, 2)(2, 3)(4)(5))




    // 相当于就是合并数据的过程
    const curry = (fn) => {
      let judge = (...args) => {
        // 判断args长度是否和fn的参数个数相等
        if (args.length === fn.length) return fn(...args) //相等证明就接收到了全部参数直接调求和函数求和
        return (...arg) => judge(...args, ...arg) //继续返回函数去接收参数
      }
      return judge  //返回一个函数去接收其他参数
    }
    function sum(a, b, c) {
      return a + b + c
    }
    let sumFn = curry(sum)
    // console.log(sumFn(1)(2)(3)) //6
    console.log(sumFn(1)(2, 3)) //6
  </script>
</body>

</html>